package com.test.jmx.jobss;

import java.io.IOException;
import java.util.Hashtable;

import javax.management.InstanceNotFoundException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.InitialContext;

public class Client {

	private MBeanServerConnection getMBeanClient(String className, ObjectName objName) throws Exception {
		MBeanServerConnection client = null;

		client = getClient();

		try {
			client.getMBeanInfo(objName);
		} catch (InstanceNotFoundException ex) {
			client.createMBean(className, objName);
		}

		// client.addNotificationListener(objName, new SlamLoadListener(), null,
		// null);

		return client;
	}

	public static MBeanServerConnection getClient() {
		MBeanServerConnection mbsc = null;

		try {
			//JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1090/server");
			JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1090/jmxconnector");
			
			JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
			mbsc = jmxc.getMBeanServerConnection();
			//jmxc.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return mbsc;
	}

	public void run(String[] args) throws IOException, MalformedObjectNameException, NullPointerException, InstanceNotFoundException,
			MBeanRegistrationException {
		MBeanServerConnection client = null;
		ObjectName objName = null;

		try {

			String mbeanName = "jboss:name=Config";
			String className = "com.test.jmx.jobss.Config";

			// SLAM_LOAD_IMPL=com.hp.sqm.slam.repository.commandtool.impl.SlamLoadCommandLine
			// SLAM_LOAD_MBEAN=SlamAgent:name=slamLoadCommandLine

			objName = new ObjectName(mbeanName);
			client = getMBeanClient(className, objName);

			client.invoke(objName, "showCount", null, null);
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (client != null)
				client.unregisterMBean(objName);
		}
	}

	public static void main(String[] args) {
		try {
			new Client().run(args);
		} catch (Exception e) {
			e.printStackTrace();
		}

//		Hashtable<String, String> props = new Hashtable<String, String>();
//
//		props.put(InitialContext.PROVIDER_URL, "jnp://localhost:1099");
//
//		props.put(InitialContext.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
//
//		MBeanServerConnection mBeanConnection;
//		try {
//			mBeanConnection = (MBeanServerConnection) new InitialContext(props).lookup("jmx/invoker/RMIAdaptor");
//			mBeanConnection.invoke(new ObjectName("jobss:name=Config,type=Service"), "showCount", null, null);
//
//		} catch (Exception e) {
//			e.printStackTrace();
//		}

	}
}
